/*
input case:

TRGSJ
XDOKI
M VLN
WPABE
UQHCF
ZA
0
ABCDE
FGHIJ
KLMNO
PQRS 
TUVWX
AA
AA
ABB
RRRLL0
ABCDE
FGHIJ
KLMNO
PQRS 
TUVWX
AAA
LLLL0
Z
*/
#include <iostream>
#include <stdio.h>

#include<string.h>
#include<ctype.h>

#include <math.h>
#include<time.h>

using namespace std;

#define maxn 100
char arr[maxn];

// temp
#define lenPuzzle 5
char arrPuzzle[lenPuzzle][lenPuzzle];

void getXY(char c, int &x, int &y) {
    switch(c) {
    case 'A': {
        y--;
        break;
    }
    case 'B': {
        y++;
        break;
    }
    case 'L': {
        x--;
        break;
    }
    case 'R': {
        x++;
        break;
    }
    default:
		x = -1; // illegal
    }
}

bool isXYValid(int x, int y) {
    if (x>=0 && y >=0 &&x<lenPuzzle &&y <lenPuzzle) {
        return true;
    } else {
        return false;
    }
}

int main() {

    // redirect in outw
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif // TEST

    int i, j, k; // loop
    int temp, len, count=0, numberResult=0; // temp, count
    int a,b, t; // cin

    // result
    bool isFirstLine= true;
    bool isFirstSpace= true;
    double result=0;
    int r1, r2,r3;

    // temp
    int lenTemp, numberPuzzleLine=0;
    bool isPuzzleEnd = false, isPuzzleHasResult = true;
    int x,y, preX, preY;


//    memset(arr, 0, sizeof(arr));

//    cin >> t;
//    while (t-- > 0)
//    scanf("%s", arr);

    while(gets(arr) != NULL && strcmp(arr,"Z") != 0) {
        // in
//        scanf("%d", &aaa);
//        scanf("%s", arr);

        // init

        // start
        numberPuzzleLine=0;
        strcpy(arrPuzzle[numberPuzzleLine++], arr);
        while (numberPuzzleLine <lenPuzzle && gets(arr) != NULL) {
            strcpy(arrPuzzle[numberPuzzleLine++], arr);
        }
        for (i = 0; i< lenPuzzle; i++) {
            for (j = 0; j< lenPuzzle; j++) {
                if (arrPuzzle[i][j]==' ') {
                    y=i;
                    x=j;
                    i=j=5;
                }
            }
        }

        isPuzzleEnd = false;
        isPuzzleHasResult = true;
        while (!isPuzzleEnd && gets(arr) != NULL) {
            len = strlen(arr);
            for (i=0; i<len; i++) {
                if (arr[i] != '0') {
                    if (isPuzzleHasResult) {
                        preX = x, preY = y;
                        getXY(arr[i], x,y);
                    }
                    if (isPuzzleHasResult) {
                        if (isXYValid(x,y)) {
                            swap(arrPuzzle[y][x], arrPuzzle[preY][preX]);
                        } else {
                            isPuzzleHasResult = false;
                        }
                    }
                } else {
                    isPuzzleEnd = true;
                    break;
                }
            }

        }

        // output format
        if (isFirstLine) isFirstLine = false;
        else cout<< endl;

        cout << "Puzzle #" <<++numberResult<<":"<<endl;
        if (isPuzzleHasResult) {
            for (i = 0; i< lenPuzzle; i++) {
                isFirstSpace = true;
                for (j=0; j<lenPuzzle; j++) {
                    if (isFirstSpace) isFirstSpace = false;
                    else cout<< " ";
                    cout << arrPuzzle[i][j];
                }
                cout<<endl;
            }
        } else {
            cout << "This puzzle has no final configuration." << endl;
        }

    }


    return 0;
}


